
package com.simplesoftwares.client.library.appskeleton;

import java.util.Vector;

import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.DisclosurePanel;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.InlineLabel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.UIObject;
import com.google.gwt.user.client.ui.VerticalPanel;


/**
 * The Class AppSkeleton.Creates the whole navigational skeleton of the app Sidebar,Appheaderbar,AppLevel menu and NavigationalBar
 * </br>
 * To Do extensive use of flow panel which means to many nested divs in generated HTML. can be simplified again not any practical
 * benifets though.
 */
public class AppSkeleton 
{
	
	/** The navigation panel.
	 * To Do :On an urgent basis sumit should find an athestically pleasing way to display user name and pic
	 *  same as that in MyErp.
	 */
	private FlowPanel navigationPanel;
	
	/** The side bar panel. */
	private FlowPanel sideBarPanel;
	
	/** The app header bar panel.
	*/
	private FlowPanel appHeaderBarPanel;
	
	/** The app level menu panel.
	 * To DO: On an urgent basis sumit should integrate icons
	 * */
	private FlowPanel appLevelMenuPanel;
	
	/** The process name. */
	private  InlineLabel processName;
	
	/** 
	 * Client company name should be initalized from database.
	 *  */
	@SuppressWarnings("unused")
	private  InlineLabel companyName;
	/**
	 * Our company Logo will be initalized from database.
	 */
	@SuppressWarnings("unused")
	private Image ourCompanyLogo;
	
	/**
	 * The client company logo of company to be displayed on AppHeaderBar.Will be initalized from Database
	 */
	@SuppressWarnings("unused")
	private  Image clientCompanyLogo;
	
	/** The navigation panel pct. */
	private int navigationPanelpct;
	
	/** The sidebar panel pct. */
	private int sidebarPanelpct;
	
	/** The app header bar panel pct. */
	private int appHeaderBarPanelpct; 
	
	/** Since each screen will provide its own logic when clicked on AppLevel menus.The Reference to current handler
	 * needs to be saved */
	public HandlerRegistration[] registration;
	
	/** The menu labels.
	 * Needed to set app level menu from outside
	 * To Do : Bad design this should not be an attribute,pradnya and sumit should change this.
	 *  */
	public  InlineLabel[]menuLabels;
	
	
	/** The event handler object.Drives the screen transition. 
	 * 
	 * */
	private SkeletonEventHandling eventHandler;
	
	/** The logout label 
	 * To Do : Same as navigational panel
	 * */
	private static Label  logout;

    /**
     * Instantiates a new AppSkeleton.
     */
    private AppSkeleton()
    {
    	eventHandler=new SkeletonEventHandling();
    	navigationPanel=new FlowPanel();
    	sideBarPanel=new FlowPanel();
    	appLevelMenuPanel=new FlowPanel();
    	appHeaderBarPanel = new FlowPanel();	
    }
    
    /**
     * Instantiates a new app skeleton.
     *
     * @param navper the navper percentage width of navigational bar panel
     * @param sidebarper the sidebarper  percentage width of sidebar bar panel
     * @param appheaderbarper the appheaderbarper percentage width of app header  bar panel
     */
    public AppSkeleton(int navper,int sidebarper,int appheaderbarper)
    {
    	this();
    	this.navigationPanelpct=navper;
    	this.sidebarPanelpct=sidebarper;
    	this.appHeaderBarPanelpct=appheaderbarper;
    	
    }
	
///////////////////////////////////////////NAVIGATION PANEL CREATION///////////////////////////////////
	/**
 * Creates the navigation panel.
 * 
 * @param navigationNames menuitems vector refrence
 */
public void createNavigationPanel( Vector<MenuItems> navigationNames)
	{
		
		navigationPanel.getElement().setId("navigationbar");
		Label lbl;
		for(int i =0;i<navigationNames.size();i++)
		{
			if(navigationNames.get(i)!=null)
			{
			lbl=(Label) navigationNames.get(i).getLabel();
			lbl.getElement().setClassName("navigationitems");
			navigationPanel.add(lbl);
			lbl.addClickHandler(eventHandler);
			}
		}
		
		logout = new Label("Logout");
		logout.getElement().setId("logout");
		navigationPanel.add(logout);
		logout.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				Window.Location.reload();
				
			}
		});
	}

////////////////////////////////////////////////SIDEBAR PANEL CREATION///////////////////////////////////
	/**
 * Creates the side bar panel with {@link MenuItems}.The actual widget type of {@link MenuItems} can be any {@link UiHandler}
 * which implements {@link HasClickHandlers}.This is because all Side bar items should be clickable
 * 
 * @param sideBarNames Sidebaritems vector which will go on sidebar.
 */
public void createSideBarPanel(Vector<SideBarItems> sideBarNames)
	{
		sideBarPanel.getElement().setId("sidebar");
		
		/*String imagePath = GWT.getHostPageBaseURL()+"images/companylogo.png";
		clientCompanyLogo=new Image(imagePath);
		setImageInSideBarPanel(clientCompanyLogo,"clientimage",160,70,"http://www.google.com/");
		companyName= new InlineLabel("Company Name");
		companyName.getElement().setId("clientcompanyname");
		
		sideBarPanel.add(companyName);
		sideBarPanel.add(new HTML("<hr/>"));
	
		
		imagePath = GWT.getHostPageBaseURL()+"images/companylogo.png";
		ourCompanyLogo=new Image(imagePath);
		setImageInSideBarPanel(ourCompanyLogo,"ourimage",160,90,"http://www.evasoftwaresolutions.com/");*/
		
		
	
		VerticalPanel vp;
		
		
		
		
		for(int i=0;i<sideBarNames.size();i++)
		{
			ScrollPanel scroll = new ScrollPanel();
			scroll.setHeight("70px");
			scroll.setWidth("200px");
			vp=new VerticalPanel();
			Vector<MenuItems> menus=sideBarNames.get(i).getMenus();
			HasClickHandlers clickable;
			for(int j=0;j<menus.size();j++)
			{
			    if(menus.get(j)!=null)
			    {
				clickable =(HasClickHandlers) menus.get(j).getLabel();
			    ((UIObject) clickable).getElement().setClassName("leftbaritems");
				clickable.addClickHandler(eventHandler);
				vp.add(menus.get(j).getLabel());
			    }
			}
			DisclosurePanel discPanel=new DisclosurePanel(sideBarNames.get(i).getTitle());
			scroll.add(vp);
			discPanel.add(scroll);
			
			sideBarPanel.add(discPanel);
			sideBarPanel.add(new HTML("<hr/>"));
			discPanel.setAnimationEnabled(true);
			
			sideBarPanel.setWidth("100%");
			
		}
		
		
	}

////////////////////////////////////////APPHEADER BAR CREATION////////////////////////////////////////////////
/**
 * Creates the AppHeaderBar panel.
 *
 * @param menuNames the menu names String array containing app level menus
 */
public void createAppHeaderBarPanel(String[] menuNames)
	{
		createAppLevelMenuPanel(menuNames);
		appHeaderBarPanel.getElement().setId("appheaderbar");
		processName= new InlineLabel("Process Name");
		
		processName.addClickHandler(eventHandler);
		processName.getElement().setId("processname");
		appHeaderBarPanel.add(processName);
		
		appHeaderBarPanel.add(appLevelMenuPanel);
		appHeaderBarPanel.setWidth("100%");
		
	}

///////////////////////////////////////////////APPLEVELMENU CREATION////////////////////////////////////////
/**
 * Creates the AppLevelMenu panel.
 * To Do: The AppLevelMenus now are {@link InlineLabel}.Change them to Icons
 *
 * @param menuNames the menu names String array containing AppLevelMenus
 */
public void createAppLevelMenuPanel(String[] menuNames)
	{
		menuLabels= new InlineLabel[menuNames.length];
		registration=new HandlerRegistration[menuNames.length];
		for(int i=0;i<menuNames.length;i++)
		{
			
			menuLabels[i] = new InlineLabel(menuNames[i]);
			appLevelMenuPanel.add(menuLabels[i]);
			
			if(i<2)
				menuLabels[i].getElement().setClassName("leftfloatbuttons");
		
			else
				menuLabels[i].getElement().setClassName("rightfloatbuttons");
			
			if(i==0)
				menuLabels[i].getElement().getStyle().setMarginLeft(20, Unit.PX);
			
			if(i==2)
				menuLabels[i].getElement().getStyle().setMarginRight(20, Unit.PX);
		
		}
		
		appLevelMenuPanel.setWidth("100%");	
	}
	
	/**
	 * *****************************GETTER SETTERS******************.
	 *
	 * @return the NavigationalPanel
	 */

	public FlowPanel getNavigationPanel() {
		return navigationPanel;
	}

	/**
	 * Sets the NavigationalPanel.
	 *
	 * @param navigationPanel the new navigation panel
	 */
	public void setNavigationPanel(FlowPanel navigationPanel) {
		this.navigationPanel = navigationPanel;
	}

	/**
	 * Gets the SideBarPanel.
	 *
	 * @return the SideBarPanel
	 */
	public FlowPanel getSideBarPanel() {
		return sideBarPanel;
	}

	/**
	 * Sets the SideBarPanel.
	 *
	 * @param SideBarPanel the new SideBarPanel
	 */
	public void setSideBarPanel(FlowPanel sideBarPanel) {
		this.sideBarPanel = sideBarPanel;
	}

	/**
	 * Gets the AppHeaderBarPanel.
	 *
	 * @return the AppHeaderBarPanel
	 */
	public FlowPanel getAppHeaderBarPanel() {
		return appHeaderBarPanel;
	}

	/**
	 * Sets the AppHeaderBarPanel.
	 *
	 * @param appHeaderBarPanel the new AppHeaderBarPanel
	 */
	public void setAppHeaderBarPanel(FlowPanel appHeaderBarPanel) {
		this.appHeaderBarPanel = appHeaderBarPanel;
	}

	/**
	 * Gets the AppLevelMenuPanel.
	 *
	 * @return the AppLevelMenuPanel
	 */
	public FlowPanel getAppLevelMenuPanel() {
		return appLevelMenuPanel;
	}

	/**
	 * Sets the AppLevelMenuPanel.
	 *
	 * @param AppLevelMenuPanel the new AppLevelMenuPanel
	 */
	public void setAppLevelMenuPanel(FlowPanel appLevelMenuPanel) {
		this.appLevelMenuPanel = appLevelMenuPanel;
	}

	/**
	 * Gets the process name.
	 *
	 * @return the process name
	 */
	public InlineLabel getProcessName() {
		return processName;
	}

	/**
	 * Sets the process name.
	 *
	 * @param processName the new process name
	 */
	public void setProcessName(InlineLabel processName) {
		this.processName = processName;
	}

	
	/**
	 * Gets the navigation panelpct.
	 *
	 * @return the navigation panelpct
	 */
	public int getNavigationPanelpct() {
		return navigationPanelpct;
	}

	/**
	 * Sets the navigation panelpct.
	 *
	 * @param navigationPanelpct the new navigation panelpct
	 */
	public void setNavigationPanelpct(int navigationPanelpct) {
		this.navigationPanelpct = navigationPanelpct;
	}

	/**
	 * Gets the sidebar panelpct.
	 *
	 * @return the sidebar panelpct
	 */
	public int getSidebarPanelpct() {
		return sidebarPanelpct;
	}

	/**
	 * Sets the sidebar panelpct.
	 *
	 * @param sidebarPanelpct the new sidebar panelpct
	 */
	public void setSidebarPanelpct(int sidebarPanelpct) {
		this.sidebarPanelpct = sidebarPanelpct;
	}

	/**
	 * Gets the app header bar panelpct.
	 *
	 * @return the app header bar panelpct
	 */
	public int getAppHeaderBarPanelpct() {
		return appHeaderBarPanelpct;
	}

	/**
	 * Sets the app header bar panelpct.
	 *
	 * @param appHeaderBarPanelpct the new app header bar panelpct
	 */
	public void setAppHeaderBarPanelpct(int appHeaderBarPanelpct) {
		this.appHeaderBarPanelpct = appHeaderBarPanelpct;
	}

	/**
	 * Gets the registration.
	 *
	 * @return the registration
	 */
	public HandlerRegistration[] getRegistration() {
		return registration;
	}

	/**
	 * Sets the registration.
	 *
	 * @param registration the new registration
	 */
	public void setRegistration(HandlerRegistration[] registration) {
		this.registration = registration;
	}

	
	/**
	 * Gets the menu labels.
	 *
	 * @return the menu labels
	 */
	public InlineLabel[] getMenuLabels() {
		return menuLabels;
	}

	/**
	 * Sets the menu labels.
	 *
	 * @param menuLabels the new menu labels
	 */
	public void setMenuLabels(InlineLabel[] menuLabels) {
		this.menuLabels = menuLabels;
	}
	
	/**
	 * Gets the event handler.
	 *
	 * @return the event handler
	 */
	public SkeletonEventHandling getEventHandler() {
		return eventHandler;
	}

	/**
	 * Sets the event handler.
	 *
	 * @param eventHandler the new event handler
	 */
	public void setEventHandler(SkeletonEventHandling eventHandler) {
		this.eventHandler = eventHandler;
	}
	
	public void setImageInSideBarPanel(Image image,String idName,int widthPx,int heightPx,final String link)
	{
		
		image.setPixelSize(widthPx,heightPx);
		sideBarPanel.add(image);
		
		image.getElement().setId(idName);
		image.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				Window.open(link,"_blank","");
				
			}
		});
		
	}
}
